package cn.gxufe.reliable.message.admin.mapper;

import cn.gxufe.reliable.message.admin.domain.RMessage;
import cn.gxufe.reliable.message.common.Message;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
/**
 * @author yingjie.rong
 */
public interface MessageMapper extends BaseMapper<RMessage> {

    /**
     * selectCanConfirm
     * @param endTime
     * @return
     */
    @Select("select id , `key` , header, data ,source_service as sourceService , try_confirm as tryConfirm" +
            " from  message where status = "+ Message.STATUS_PREPARE +" and is_done = 0  and  prepare_time < #{endTime} limit 50")
    List<RMessage> selectCanConfirm(@Param("endTime") java.util.Date endTime );

    /**
     * selectCanConfirm
     * @param endTime
     * @return
     */
    @Select("select id , `key` ,header , data ,finish_data as finishData ,source_service as sourceService " +
            " from  message where status = "+ Message.STATUS_FINISH +" and is_done = 0 and finish_time < #{endTime}  limit 50 ")
    List<RMessage> selectCanFinish(@Param("endTime") java.util.Date endTime );

    /**
     * confirmUpdate
     * @param status
     * @param id
     * @param oldStatus
     * @return
     */
    @Update("update message set status =#{status}, try_confirm = try_confirm + 1 , confirm_time = CURRENT_TIMESTAMP where status=#{oldStatus} and id = #{id} ")
    int confirmUpdate(@Param("status") Integer status ,
                      @Param("id") Long id,
                      @Param("oldStatus") Integer oldStatus);


    /**
     * cancelMessageForConfirm
     * @param status
     * @param id
     * @param oldStatus
     * @return
     */
    @Update("update message set status =#{status},try_confirm = try_confirm + 1 , confirm_time = CURRENT_TIMESTAMP" +
            "  where status=#{oldStatus} and id = #{id} and try_confirm > 5")
    int cancelMessageForConfirm(@Param("status") Integer status ,
                                @Param("id") Long id,
                                @Param("oldStatus") Integer oldStatus);




    /**
     * sendUpdate
     * @param status
     * @param id
     * @return
     */
    @Update("update message set try_send = try_send + 1 , send_time = CURRENT_TIMESTAMP" +
            " where status =#{status} and id = #{id}")
    int updateForSend(@Param("status") Integer status ,@Param("id") Long id);

    /**
     * 待投递消息
     * selectCanSend
     * @param endTime
     * @return
     */
    @Select("select `key`,header,data,status,id from message " +
            "where  is_done = 0  and  status = " + Message.STATUS_CONFIRM +" and  send_time < #{endTime} limit 50")
    List<RMessage> selectCanSend(@Param("endTime") java.util.Date endTime );



    /**
     * finishUpdate
     * @param status
     * @param id
     * @param newDone
     * @param oldDone
     * @return
     */
    @Update("update message set try_finish = try_finish + 1 , is_done = #{newDone} , finish_time = CURRENT_TIMESTAMP" +
            " where status =#{status} and is_done = #{oldDone} and id = #{id}")
    int updateFinish(@Param("status") Integer status ,
                     @Param("id") Long id,
                     @Param("newDone") Integer newDone,
                     @Param("oldDone") Integer oldDone);


    /**
     * updateFinishOrCancel
     * @param newStatus
     * @param oldStatus
     * @param finishData
     * @param targetService
     * @param id
     * @return
     */
    @Update("update message set target_service = #{target_service}, finish_time = CURRENT_TIMESTAMP ,finish_data = #{finish_data} ,status = #{newStatus}" +
            " where status =#{oldStatus} and id = #{id}")
    int updateFinishOrCancel(@Param("newStatus") int newStatus, @Param("oldStatus") int oldStatus,
                             @Param("finish_data") String finishData,
                             @Param("target_service") String targetService ,@Param("id") Long id);

    /**
     * selectTryFinishOrCancel
     * @param date
     * @return
     */
    @Select("select id , `key` ,header , target_service as targetService , finish_data as finishData" +
            " from  message where status = "+ RMessage.STATUS_TRY_FINISH +" and is_done = 0 and finish_time < #{endTime}  limit 50 ")
    List<RMessage> selectTryFinishOrCancel(Date date);


    /**
     * selectCancel
     * @param date
     * @return
     */
    @Select("select id , `key` ,header , source_service as sourceService , finish_data as finishData" +
            " from  message where status = "+ Message.STATUS_CANCEL +" and is_done = 0 and finish_time < #{endTime}  limit 50 ")
    List<RMessage> selectCancel(Date date);

}
